//
// Created by 11510 on 2024/6/13.
//
#include <iostream>
using namespace std;


class Singleton{
public:
    class AutoRelease{
        public:AutoRelease(){
            printf("AutoRelease()");
        }

        public:~AutoRelease(){
            destroy();
        }
    };
private:
    int _ix;
    int _iy;
    static Singleton * _pInstance;
    /**
     * 静态类销毁时会自动调用析构函数，从而回收外部类对象
     */
    static AutoRelease autoRelease;



public:
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton(0, 0);
        }
        return _pInstance;
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }

    void init(int x,int y){
        _ix = x;
        _iy = y;
    }

    void print(){
        cout << "(" << this->_ix
             << "," << this->_iy
             << ")" << endl;
    }


private:
    Singleton() = default;//C++11
    Singleton(int x,int y): _ix(x), _iy(y){
        cout << "Singleton(int,int)" << endl;
    }
    ~Singleton(){
        cout << "~Singleton()" << endl;
    }
    Singleton(const Singleton & rhs) = delete;
    Singleton & operator=(const Singleton & rhs) = delete;
};

Singleton * Singleton::_pInstance = nullptr;
Singleton::AutoRelease Singleton::autoRelease;



void test0(){
    Singleton::getInstance()->init(2, 9);
    Singleton::getInstance()->print();
//    Singleton::destroy();
}



int main(void) {
    test0();
    return 0;
}
